package com.hy.aicodegeneration.langgraph4j.ai;

import com.hy.aicodegeneration.langgraph4j.tools.ImageSearchTool;
import com.hy.aicodegeneration.langgraph4j.tools.LogoGeneratorTool;
import com.hy.aicodegeneration.langgraph4j.tools.MermaidDiagramTool;
import com.hy.aicodegeneration.langgraph4j.tools.UndrawIllustrationTool;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片收集服务工厂
 */
@Slf4j
@Configuration
public class ImageCollectionServiceFactory {

    @Resource(name = "openAiChatModel")
    private ChatModel chatModel;

    @Resource
    private ImageSearchTool imageSearchTool;

    @Resource
    private UndrawIllustrationTool undrawIllustrationTool;

    @Resource
    private MermaidDiagramTool mermaidDiagramTool;

    @Resource
    private LogoGeneratorTool logoGeneratorTool;

    /**
     * 创建图片收集 AI 服务
     */
    @Bean
    public ImageCollectionService createImageCollectionService() {
        // 动态构建工具列表
        List<Object> tools = new ArrayList<>();
        tools.add(imageSearchTool);
        tools.add(undrawIllustrationTool);
        tools.add(logoGeneratorTool);
        
        // 只有当 mermaidDiagramTool 存在时才添加
        if (mermaidDiagramTool != null) {
            tools.add(mermaidDiagramTool);
            log.info("MermaidDiagramTool 成功加载到 ImageCollectionService");
        } else {
            log.info("MermaidDiagramTool 未加载（可能由于缺少 COS 配置），将在 ImageCollectionService 中忽略");
        }

        return AiServices.builder(ImageCollectionService.class)
                .chatModel(chatModel)
                .maxSequentialToolsInvocations(30)
                .tools(tools)
                .build();
    }
}